package cn.itheima.student.service.impl;

import java.util.List;

import cn.itheima.student.dao.StudentDao;
import cn.itheima.student.dao.impl.StudentDaoImpl;
import cn.itheima.student.daomain.PageBean;
import cn.itheima.student.daomain.Student;
import cn.itheima.student.service.StudentService;

public class StudentServiceImpl implements StudentService {

	public List<Student> findAllStudent() {
		StudentDao dao = new StudentDaoImpl();
		List<Student> list = dao.findAll();
		
		return list;
		
	}

	public void add(Student stu) {
		StudentDao dao = new StudentDaoImpl();
		dao.addStudent(stu);
	}

	public void deleteStudentBySid(String sid) {
		StudentDao dao = new StudentDaoImpl();
		dao.deleteStudent(sid);
	}

	public List<Student> findStudentById(int sid) {
		StudentDao dao = new StudentDaoImpl();
		List<Student> list = dao.findOne(sid);
		return list;
	}

	public void updateStudent(Student stu) {
		StudentDao dao = new StudentDaoImpl();
		dao.updateStudent(stu);
	}

	//分页方法
	public PageBean getPage(String currentPage) {
				// 1.封装每页显示的条数
				PageBean bean = new PageBean();
				int pageSize = 3;
				bean.setPageSize(pageSize);
				//2.封装当前的页码数
				int currPage = Integer.parseInt(currentPage);
				bean.setCurrentPage(currPage);
				//3.封装每页显示的数据
				StudentService service = new StudentServiceImpl();
				int begin = (currPage-1)*pageSize;
				List<Student> list = service.findPageStudent(begin,pageSize);
				bean.setList(list);
				//4.封装总条数
				int totalCount=0;
				Long dl = service.findCount();
				totalCount = dl.intValue();
				bean.setTotalCount(totalCount);
				//5.封装的总页码数
				int totalPage =0;
				double ttcount = totalCount;
				Double tp = Math.ceil(ttcount/pageSize);
				totalPage = tp.intValue();
				bean.setTotalPage(totalPage);
				return bean;
	}

	public List<Student> findPageStudent(int begin, int pageSize) {
		StudentDao dao = new StudentDaoImpl();
		List<Student> list = dao.findByPage(begin, pageSize);
		return list;
	}

	public Long findCount() {
		StudentDao dao = new StudentDaoImpl();
		long dou = dao.findTotalCount();
		
		return dou;
	}

	public List<Student> findByName(String sname) {
		
		StudentDao dao = new StudentDaoImpl();
		
		List<Student> list = dao.findMoHu(sname);
		
		return list;
	}


}
